package org.batch.utils;

import cn.hutool.core.util.StrUtil;
import org.cleaver.object.number.IntegerUtil;
import org.cleaver.reflect.FieldUtil;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;

/**
 * spring-boot-batch项目工具类
 *
 * @author LiKun
 * @date 2021/9/29 16:32
 */
public final class ObjectUtil {
    public static final String ID = "id";
    public static final String COMMA = ",";
    public static final String LONG = "Long";
    public static final String DATE = "Date";
    public static final String STRING = "String";
    public static final String DOUBLE = "Double";
    public static final HashMap<Class<?>, String> JOB_CLASSES = new HashMap<>(8);

    static {
        JOB_CLASSES.put(int.class, LONG);
        JOB_CLASSES.put(long.class, LONG);
        JOB_CLASSES.put(Long.class, LONG);
        JOB_CLASSES.put(Integer.class, LONG);

        JOB_CLASSES.put(double.class, DOUBLE);
        JOB_CLASSES.put(Double.class, DOUBLE);

        JOB_CLASSES.put(Date.class, DATE);
        JOB_CLASSES.put(String.class, STRING);
    }

    /**
     * 将给定的字符串解析为字符串数组
     *
     * @param target 给定的字符串
     *
     * @return 字符串数组
     */
    public static String[] parse2Array(String target) {
        return target.contains(COMMA) ? Arrays.stream(target.split(COMMA)).map(String::trim).toArray(String[]::new) : new String[]{target};
    }

    /**
     * 获取给定类(及其所有父类)的字段名称
     *
     * @param tClass 给定类行
     *
     * @return 字段名称
     */
    public static String[] fieldsWithSuper(Class<?> tClass) {
        return Arrays.stream(FieldUtil.fieldsWithSuper(tClass)).map(Field::getName).toArray(String[]::new);
    }

    /**
     * 获取给定ID的JobParametersBuilder对象
     *
     * @param id id标识
     *
     * @return JobParametersBuilder对象
     */
    public static JobParametersBuilder jobParametersBuilder(long id) {
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        return jobParametersBuilder.addLong(ID, id);
    }

    /**
     * 根据给定的参数构建JobParameters
     *
     * @param id     ID标识
     * @param params 相关参数
     *
     * @return JobParameters
     *
     * @throws Exception 参数数量不对应
     */
    public static JobParameters jobParameters(long id, Object... params) throws Exception {
        if (IntegerUtil.equalsZero(params.length % 2)) {
            JobParametersBuilder jobParametersBuilder = jobParametersBuilder(id);
            for (int i = 0; i < params.length; i++) {
                buildParameters(jobParametersBuilder, String.valueOf(params[i++]), params[i]);
            }
            return jobParametersBuilder.toJobParameters();
        } else {
            throw new Exception("参数数量不对应");
        }
    }

    /**
     * 判断给定数据的类型并构建JobParameters
     *
     * @param jobParametersBuilder JobParameters构造器
     * @param key                  参数Key
     * @param value                参数值Value
     */
    public static void buildParameters(JobParametersBuilder jobParametersBuilder, String key, Object value) {
        String type = JOB_CLASSES.get(value.getClass());
        if (StrUtil.isEmpty(type)) {
            jobParametersBuilder.addString(key, String.valueOf(value));
        } else {
            switch (type) {
                case DATE:
                    jobParametersBuilder.addDate(key, (Date) value);
                    break;
                case LONG:
                    jobParametersBuilder.addLong(key, Long.valueOf(String.valueOf(value)));
                    break;
                case DOUBLE:
                    jobParametersBuilder.addDouble(key, Double.valueOf(String.valueOf(value)));
                    break;
                default:
                    jobParametersBuilder.addString(key, String.valueOf(value));
            }
        }
    }
}
